/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.emergency.service.impl;

import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.emergency.domain.EmergencyInstitutions;
import com.koron.emergency.domain.EmergencyInstitutionsStaff;
import com.koron.emergency.mapper.EmergencyInstitutionsMapper;
import com.koron.emergency.mapper.EmergencyInstitutionsStaffMapper;
import com.koron.emergency.service.api.EmergencyInstitutionsStaffService;
import com.koron.emergency.util.BusinessCode;
import com.koron.workflow.bean.MessageEntity;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

@Service
public class EmergencyInstitutionsStaffServiceImpl implements EmergencyInstitutionsStaffService {
    private final static  Logger logger= LoggerFactory.getLogger(EmergencyInstitutionsStaffServiceImpl.class);

    @Override
    public MessageBean<Integer> save(StaffBean user, EmergencyInstitutionsStaff staff) {
        MessageBean<Integer> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),"保存成功",Integer.class);
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(staff.getStaffName())){
                msg.setCode(BusinessCode.STAFF_EMPTY_STAFF.getCode());
                msg.setDescription(BusinessCode.STAFF_EMPTY_STAFF.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(staff.getDepartmentName())){
                msg.setCode(BusinessCode.STAFF_EMPTY_DEPARTMENT.getCode());
                msg.setDescription(BusinessCode.STAFF_EMPTY_DEPARTMENT.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(staff.getInstitutionsId())){
                msg.setCode(BusinessCode.STAFF_EMPTY_EMERGENCYINSTITUTIONS.getCode());
                msg.setDescription(BusinessCode.STAFF_EMPTY_EMERGENCYINSTITUTIONS.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(staff.getDutyDescription())){
                msg.setCode(BusinessCode.STAFF_EMPTY_DUTY.getCode());
                msg.setDescription(BusinessCode.STAFF_EMPTY_DUTY.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(staff.getRole())){
                msg.setCode(BusinessCode.STAFF_EMPTY_ROLE.getCode());
                msg.setDescription(BusinessCode.STAFF_EMPTY_ROLE.getDescription());
                return msg;
            }
            EmergencyInstitutions emergencyInstitutions=factory.getMapper(EmergencyInstitutionsMapper.class).queryOne(staff.getInstitutionsId());
            if (null==emergencyInstitutions){
                msg.setCode(BusinessCode.EMERGENCYINSTITUTIONS_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.EMERGENCYINSTITUTIONS_NOT_EXIST.getDescription());
                return msg;
            }
            Integer value=0;
            if (StringUtils.isEmpty(staff.getId())){
                //新增
                staff.setId(DefaultIdGenerator.getInstance().generateLongId());
                staff.setAddTime(staff.getUpdateTime());
                staff.setAddUser(staff.getUpdateUser());
                staff.setAddUserName(staff.getUpdateUserName());
                value=factory.getMapper(EmergencyInstitutionsStaffMapper.class).insert(staff);
            }else{
                //修改
                EmergencyInstitutionsStaff entity=queryOne(staff.getId()).getData();
                if (null==entity){
                    msg.setCode(BusinessCode.STAFF_NOT_EXIST.getCode());
                    msg.setDescription(BusinessCode.STAFF_NOT_EXIST.getDescription());
                    return msg;
                }
                value=factory.getMapper(EmergencyInstitutionsStaffMapper.class).update(staff);
            }
            if (value>0){
                msg.setData(value);
                return msg;
            }else {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("保存失败");
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> delete(String id) {
        MessageBean<Integer> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),"删除成功",Integer.class);
        try (SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            EmergencyInstitutionsStaff entity=queryOne(id).getData();
            if (null==entity){
                msg.setCode(BusinessCode.STAFF_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.STAFF_NOT_EXIST.getDescription());
                return msg;
            }
            Integer value=factory.getMapper(EmergencyInstitutionsStaffMapper.class).delete(id);
            if (value>0){
                msg.setData(value);
                return msg;
            }else{
                msg.setData(value);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("删除失败");
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<EmergencyInstitutionsStaff> queryOne(String id) {
        MessageBean<EmergencyInstitutionsStaff> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),"查询详情成功",EmergencyInstitutionsStaff.class);
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(id)){
                msg.setCode(BusinessCode.NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            EmergencyInstitutionsStaff emergencyInstitutionsStaff=factory.getMapper(EmergencyInstitutionsStaffMapper.class).queryOne(id);
            msg.setData(emergencyInstitutionsStaff);
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<EmergencyInstitutionsStaff>> list(String institutionsId, String staffName, Integer start, Integer rows) {
        MessageBean<DataBean<EmergencyInstitutionsStaff>> msg=new MessageEntity<>();
        try(SessionFactory factory=new SessionFactory()){
            if (StringUtils.isEmpty(institutionsId)){
                msg.setCode(BusinessCode.NOT_CHANGE_DATA.getCode());
                msg.setDescription(BusinessCode.NOT_CHANGE_DATA.getDescription());
                return msg;
            }
            EmergencyInstitutions emergencyInstitutions=factory.getMapper(EmergencyInstitutionsMapper.class).queryOne(institutionsId);
            if (null==emergencyInstitutions){
                msg.setCode(BusinessCode.EMERGENCYINSTITUTIONS_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.EMERGENCYINSTITUTIONS_NOT_EXIST.getDescription());
                return msg;
            }

            List<EmergencyInstitutionsStaff> list=factory.getMapper(EmergencyInstitutionsStaffMapper.class).queryByInstitutionsId(institutionsId,
                    staffName,start,rows);
            Integer totalNumber=factory.getMapper(EmergencyInstitutionsStaffMapper.class).countByInstitutionsId(institutionsId,staffName);
            rows = rows == null ?totalNumber:( rows <= 0 ? 1 : rows);
            Integer totalPage = totalNumber > rows ? (totalNumber % rows == 0 ? totalNumber / rows : totalNumber / rows + 1) : 1;
            DataBean<EmergencyInstitutionsStaff> dataBean=new DataBean<>();
            dataBean.setTotalPage(totalPage);
            dataBean.setTotalNumber(totalNumber);
            dataBean.setPageSize(rows);
            dataBean.setList(list);
            msg.setData(dataBean);
            msg.setDescription("获取人员列表成功");
            msg.setCode(BusinessCode.SUCCESS.getCode());
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
}
